<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------

/**
 * 调用微电汇相关接口类
 * @package  module/eai/wdh/ebsig
 * @author   allen.qiang
 * @version 1.0
 */

class wdhEAI implements wdhInterface  {

    /**
     * 组装请求参数，并调用接口
     * @param array $api_data 接口参数
     * @param string $scope 接口
     * @return array
     */
    private function requestApi($api_data, $scope) {

        $quanliHttp = new quanliHttp();

        $system_data = array(
            'apiKey' => G_WDH_API_KEY,
            'scope' => $scope,
            'version' => 1,
            'param' => json_encode($api_data),
            'timestamp' => time(),
            'format' => 'json'
        );

        ksort($system_data);

        $sign = '';
        foreach ($system_data as $k => $v) {
            $sign .= $k .'='. $v;
        }
        $sign .= G_WDH_KEY;

        $system_data['sign'] = md5($sign);

        $result_data = $quanliHttp->post(G_WDH_GATEWAY_URL, $system_data);
        if ($result_data['code'] != 200) {
            return array( 'code' => $result_data['code'], 'message' => $result_data['message'] );
        } else if (!isset($result_data['data']) || empty($result_data['data'])) {
            return array( 'code' => 404, 'message' => '无数据' );
        }

        return $result_data['data'];

    }

    /**
     * 查询微信会员信息
     * @param string $openId 微信粉丝id
     * @param string $uid 微信粉丝uuid
     * @param string $pcustID 微信粉丝会员id
     * @return array
     */
    public function searchWechatCustomer( $openId, $uid = '', $pcustID = '' ) {

        if (!isset($openId, $uid, $pcustID)) {
            return array( 'code' => 400, 'message' => '参数错误1' );
        }
        if (empty($openId) && empty($uid) && empty($pcustID)) {
            return array( 'code' => 400, 'message' => '参数错误2' );
        }

        $api_data = array(
            'openId' => $openId,
            'wechat_uid' => $uid,
            'pcustID' => $pcustID,
        );

        return $this->requestApi( $api_data, 'wechat.customer.get' );

    }

    /**
     * 查询微信配置参数
     * @return array
     */
    public function searchWechatAppArgs() {
        return $this->requestApi( array(), 'wechat.app.args' );
    }

    /**
     * 获取微信access token
     */
    public function getAccessToken() {
        $access_token = $this->requestApi( array(), 'wechat.app.access_token' );
        if (isset($access_token['access_token'])) {
            return $access_token['access_token'];
        } else {
            return null;
        }
    }

    /**
     * 新增微信会员
     * @param array $api_data 微信会员信息
     * @return array
     */
    public function addWechatCustomer( $api_data ) {
        return $this->requestApi( $api_data, 'wechat.customer.add' );
    }

    /**
     * 修改微信会员
     * @param array $api_data 微信会员信息
     * @return array
     */
    public function alterWechatCustomer( $api_data ) {
        return $this->requestApi( $api_data, 'wechat.customer.alter' );
    }

    /**
     * 绑定微信粉丝、网站会员
     * @param array $api_data 会员信息
     * @return array
     */
    public function bindWechatCustomer($api_data) {

        if (!isset($api_data['pcustID']) || !quanli_is_int($api_data['pcustID'])) {
            return array('code' => 400, 'message' => '缺少参数：pcustID');
        }

        if (!isset($api_data['wechat_uid']) || empty($api_data['wechat_uid'])) {
            return array('code' => 400, 'message' => '缺少参数：wechat_uid');
        }

        if (!isset($api_data['custID']) || empty($api_data['custID'])) {
            return array('code' => 400, 'message' => '缺少参数：custID');
        }

        if (!isset($api_data['mobile']) || empty($api_data['mobile'])) {
            return array('code' => 400, 'message' => '缺少参数：mobile');
        }

        if (!isset($api_data['custName']) || empty($api_data['custName'])) {
            $api_data['custName'] = null;
        }

        if (!isset($api_data['email']) || empty($api_data['email'])) {
            $api_data['email'] = null;
        }

        if (!isset($api_data['rankID']) || !quanli_is_int($api_data['rankID'])) {
            return array('code' => 400, 'message' => '缺少参数：rankID');
        }

        if (!isset($api_data['rankName']) || empty($api_data['rankName'])) {
            return array('code' => 400, 'message' => '缺少参数：rankName');
        }

        if (!isset($api_data['sumAmt']) || !is_numeric($api_data['sumAmt'])) {
            $api_data['sumAmt'] = 0;
        }

        if (!isset($api_data['custSource']) || !in_array($api_data['custSource'], array(1, 2, 3, 4, 5, 6, 7))) {
            return array('code' => 400, 'message' => '缺少参数：custSource');
        }

        if (!isset($api_data['register_time']) || empty($api_data['register_time'])) {
            return array('code' => 400, 'message' => '缺少参数：register_time');
        }

        return $this->requestApi( $api_data, 'wechat.customer.bind' );

    }

    /**
     * 记录菜单点击记录
     * @param string $url 链接
     * @param string $openId 微信粉丝标识
     * @param string $keyValue 菜单值
     * @return array
     */
    public function addMenuLog($url, $openId, $keyValue) {

        $date = date('Ymd');
        $menu_id = str_replace('MENU_', '', $keyValue);
        $api_data = array(
            'url' => $url,
            'openid' => $openId,
            'tagCode' => '1.1.' . $menu_id . '.' . $date,
            'fc' => 1,
            'sc' => 1,
            'ic' => $menu_id,
            'tc' => $date,
            'wxtimeStamp' => time(),
            'st' => 1
        );
        return $this->requestApi( $api_data, 'wechat.log.menu' );

    }

}